﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class NPS_app_Monitoring_ContactPerson : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            for (int i = 0; i < 5; i++)
            {
                ddlBatch.Items.Add((DateTime.Now.Year - i).ToString());
                ddlEditBatch.Items.Add((DateTime.Now.Year - i).ToString());
            }
            ddlBatch.SelectedValue = DateTime.Now.Year.ToString();
            ddlEditBatch.SelectedValue = DateTime.Now.Year.ToString();
            loadSearchForm();
            getKategori();
            btnEdit.Enabled = false;
            pnlView.Visible = false;
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Controller.NPSTypeController tc = new Controller.NPSTypeController();
        Entities.NPSSurvey entNPS = new Entities.NPSSurvey
        {   
            iID = Convert.ToInt32(hfNPSID.Value),
            iNPSCPCategoryID = Convert.ToInt32(ddlKategori.SelectedValue)
        };
        Controller.NPSSurveyController ctrNPS = new Controller.NPSSurveyController();
        ctrNPS.UpdateNPSSurveyDataKategori(entNPS);
        txtKategori.Text = ddlKategori.SelectedItem.Text;
    }
    protected void ddlBatch_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlEditBatch.SelectedValue = ddlBatch.SelectedValue;
        if (!hfID.Value.Equals(""))
            bindData(hfID.Value);
    }
    protected void gvSearch_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "choose")
        {
            bindData(e.CommandArgument.ToString());
        }
    }
    private void loadSearchForm()
    {
        Controller.NPSCustomerContactController ncc = new Controller.NPSCustomerContactController();
        gvSearch.DataSource = ncc.NPSCustomerContactList;
        gvSearch.DataBind();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        search();
    }

    private void search()
    {
        Controller.NPSCustomerContactController ncc = new Controller.NPSCustomerContactController();
        gvSearch.DataSource = ncc.getNPSCustomerContactList(txtSearchName.Text, txtSearchCompany.Text);
        gvSearch.DataBind();
    }
    protected void bindData(string id)
    {
        Controller.NPSCustomerContactController ncc = new Controller.NPSCustomerContactController();
        Entities.NPSCustomerContact contact = ncc.getNPSCustomerContact(Convert.ToInt32(id), true);
        Clear();
        if (contact.isNPSInfoIncluded)
        {
            int surveyID = 0;
            for (int i = 0; i < contact.oNPSInfo.Count; i++)
            {
            	if (contact.oNPSInfo[i].dtmSurveyDate.Year.ToString() == ddlBatch.SelectedValue.ToString())
                {
                    surveyID = contact.oNPSInfo[i].iID;
                    hfNPSID.Value = surveyID.ToString();
                    break;
                }
            }
            if (surveyID > 0)
            {
                Controller.NPSSurveyController sc = new Controller.NPSSurveyController();
                Entities.NPSSurvey survey = sc.getNPSSurvey(surveyID, true);
                txtNPSScore.Text = survey.iScore.ToString();
                txtStatusNPS.Text = survey.oNPSType.sxTitle;
                txtKategori.Text = survey.oNPSCPCategory.sxTitle;
                if (survey.oNPSCPCategory.iID > 0)
                    ddlKategori.SelectedValue = survey.oNPSCPCategory.iID.ToString();
            }
        }

        hfID.Value = contact.iID.ToString();
        txtNama.Text = contact.sxPICName;
        txtPotition.Text = contact.sxPosition;
        txtPerusahaan.Text = contact.sxCompanyName;
        txtKelas.Text = contact.sxCompanyClass;
        txtSegment.Text = contact.sxCompanySegment;

        txtEditNama.Text = contact.sxPICName;

        pnlView.Visible = true;

        if (!txtKategori.Text.Equals(""))
            btnEdit.Enabled = true;
        else
            btnEdit.Enabled = false;
    }
    private void getKategori()
    {
        Controller.NPSCPCategoryController ctrNPS = new Controller.NPSCPCategoryController();
        List<Entities.NPSCPCategory> listNPS = new List<Entities.NPSCPCategory>();
        listNPS = ctrNPS.NPSCPCategoryList;
        ddlKategori.Items.Clear();
        ddlKategori.DataSource = listNPS;
        ddlKategori.DataBind();
    }
    private void Clear()
    {
        pnlView.Visible = false;
        hfID.Value = "";
        hfNPSID.Value = "";
        txtEditNama.Text = "";
        txtKategori.Text = "";
        txtKelas.Text = "";
        txtNama.Text = "";
        txtNPSScore.Text = "";
        txtPerusahaan.Text = "";
        txtPotition.Text = "";
        txtSearchCompany.Text = "";
        txtSearchName.Text = "";
        txtSegment.Text = "";
        txtStatusNPS.Text = "";
    }
}
